package com.guit.hrms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guit.hrms.mapper.DepartmentMapper;
import com.guit.hrms.mapper.EmployeeMapper;
import com.guit.hrms.mapper.PositionMapper;
import com.guit.hrms.mapper.SettingsMapper;
import com.guit.hrms.po.department.Department;
import com.guit.hrms.po.dept.GetDeptPerson;
import com.guit.hrms.po.utilsPO.PageBean;
import com.guit.hrms.service.DepartmentService;
import com.guit.hrms.vo.OfferVO;
import com.guit.hrms.vo.department.DepartmentVO;
import com.guit.hrms.vo.position.PositionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private SettingsMapper settingsMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 新增部门
     *
     * @param department 部门信息
     */
    @Override
    public Integer addDepartment(Department department) {
        if (department.getDeptNumber() == null
                || department.getDeptName() == null
                || department.getDeptParentId() == null
                || department.getDeptManagerId() == null
                || department.getDeptProfile() == null
                || department.getDeptLevel() == null) {
            return 2;
        }
        Department exist = departmentMapper.selectDepartmentByName(department.getDeptName());
        if (exist != null) {
            return 0;
        } else {
            department.setCreateTime(LocalDateTime.now());
            department.setUpdateTime(LocalDateTime.now());
            return departmentMapper.addDepartment(department);
        }
    }

    /**
     * 更新部门信息
     *
     * @param department 要更新的部门信息
     */
    @Override
    public Integer updateDepartment(Department department) {
        if (department.getDeptId() == null) {
            return 3;
        }
        if (department.getDeptNumber() == null
                && department.getDeptName() == null
                && department.getDeptParentId() == null
                && department.getDeptManagerId() == null
                && department.getDeptProfile() == null
                && department.getDeptLevel() == null) {
            return 2;
        }
        DepartmentVO exist = departmentMapper.selectDepartmentById(department.getDeptId());
        if (exist == null) {
            return 0;
        } else {
            department.setUpdateTime(LocalDateTime.now());
            return departmentMapper.updateDepartment(department);
        }
    }

    /**
     * 查询单个部门
     *
     * @param deptId 要查询的部门id
     */
    @Override
    public DepartmentVO selectDepartment(Integer deptId) {

        //获取部门表
        DepartmentVO departmentVO = departmentMapper.selectDepartmentById(deptId);
        //获取部门下的岗位表
        List<PositionVO> positionVOList = departmentMapper.getPositionBydeptId(deptId);
        //封装进部门
        departmentVO.setPositions(positionVOList);
        return departmentVO;
    }

    /**
     * 查询所有部门
     */
    @Override
    public List<DepartmentVO> selectDepartments() {
        //获取全部部门
        List<DepartmentVO> departmentVOList = departmentMapper.queryDepartment();
        //用增强for重复单个部门时的操作
        for (DepartmentVO departmentVO : departmentVOList) {
            //获取部门下的岗位表
            List<PositionVO> positionVOList = departmentMapper.getPositionBydeptId(departmentVO.getDeptId());
            //封装进部门
            departmentVO.setPositions(positionVOList);
        }
        return departmentVOList;
    }


    /**
     * 删除部门
     * 该方法会同时删除对应部门的员工
     *
     * @param deptIds 要删除的部门id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteDepartment(List<Integer> deptIds) {
        //获取设置
        String setting_name = "部门是否连带删除";
        Integer value = settingsMapper.getValueBySettingName(setting_name);
        //删除部门
        if (value == 0) {
            for (Integer deptId : deptIds) {
                departmentMapper.deleteDelptById(deptId);
            }
        } else if (value == 1) {
            for (Integer deptId : deptIds) {
                departmentMapper.deleteDelptById(deptId);
                //删除子部门
                departmentMapper.deleteDelptByParentId(deptId);
                //删除子岗位
                positionMapper.deletePositionByDeptId(deptId);
                //修改员工所属
                employeeMapper.setDeptAndPositionByDeptId(deptId);
            }
        }
        return 0;
    }

    /**
     * 查询部门下各个岗位的员工
     * @param page
     * @param pageSize
     * @param deptId
     * @param positionId
     * @return
     */
    @Override
    public PageBean getDeptPerson(Integer page, Integer pageSize, Integer deptId,Integer deptType, Integer positionId) {
        PageHelper.startPage(page, pageSize);

        List<GetDeptPerson> getDeptPersonList = employeeMapper.getDeptPerson(deptId,deptType, positionId);

        Page<GetDeptPerson> p = (Page<GetDeptPerson>) getDeptPersonList;
        return new PageBean(p.getTotal(), p.getResult());
    }
    //临时对象

}
